package com.runachina.common;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.runachina.R;
import com.runachina.runa.ReadMeterActivity;

/**
 * 加密算法：((mac1+mac2+mac3+mac4)%100 + 期限 + day )*2
 * <p>
 * 解密：先除以2
 */

public class LoginAcvivity extends BaseActivity {


    private Button btnlogin;
    private EditText registercode, registertime, duetime;
    private TextView macAdd;

    private static String wifimac = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        set_toolbar("注册", "");

        init_view();

        getWifiMac();//获取手机Mac地址

        displaytimeInfo();//显示注册时间信息


        if (getDaysBetween("" + readLocalCfgInt("endTimeCfg")) && (judgeTimeMode() == 1)) {
            startActivity(new Intent(LoginAcvivity.this, ReadMeterActivity.class));
            finish();//没有到期
        }
    }

    int judgeTimeMode() {
        int mode = 0;//自动时间就是1，手动时间就是0
        try {
            mode = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.AUTO_TIME);
        } catch (Exception e) {
        }
        ;
        return mode;
    }

    /**
     * 初始化界面控件
     */
    void init_view() {
        btnlogin = (Button) findViewById(R.id.btnlogin);
        btnlogin.setOnClickListener(loginFun);
        macAdd = (TextView) findViewById(R.id.mac);

        registercode = (EditText) findViewById(R.id.registercode);

        registertime = (EditText) findViewById(R.id.registertime);
        duetime = (EditText) findViewById(R.id.duetime);


    }

    /**
     * 显示注册时间 和 到期时间
     */
    void displaytimeInfo() {
        if (readLocalCfgInt("startTimeCfg") != 0) {
            registertime.setText("" + readLocalCfgInt("startTimeCfg"));
            duetime.setText("" + readLocalCfgInt("endTimeCfg"));
        }
    }


    /**
     * 获取手机Mac地址
     * <p>
     * 此处会导致内存泄漏
     */
    void getWifiMac() {
        WifiManager wifi = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        wifimac = info.getMacAddress() + "00000000000000000000";

        displayMacAdd();
    }

    /**
     * login: 按键响应事件
     */
    View.OnClickListener loginFun = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (judgeTimeMode() == 1) {
                //是否注册码正确
                if (judgeRegisterCode()) {

                    //获取注册时间-保存
                    int startTime = Integer.parseInt(getData());
                    writeLocalCfg("startTimeCfg", startTime);
                    //获取到期时间-保存
                    writeLocalCfg("endTimeCfg", getEndTime(startTime));
                    //跳转
                    startActivity(new Intent(LoginAcvivity.this, ReadMeterActivity.class));//读取抄表
                    finish();
                } else {
                    show("注册码错误!");
                }
            } else {
                show("请将手机时间改为自动!");
            }

        }
    };

    /**
     * 获取到期时间
     *
     * @param startTime
     * @return
     */
    int getEndTime(int startTime) {
        int endTime = 0;
        //获取注册码中的期限
        int regCode = Integer.parseInt("0" + registercode.getText().toString()) / 2;
        String regStr = "000000" + regCode;
        regStr = regStr.substring(regStr.length() - 6, regStr.length());
        int qixian = Integer.parseInt(regStr.substring(2, 4));


        endTime = startTime + qixian * 100;
        int day = startTime % 100;
        int mon = (endTime % 10000) / 100;
        int year = (endTime / 10000) + mon / 12;
        mon = mon % 12;

        String yearStr = "" + year;
        String monStr = "00" + mon;
        monStr = monStr.substring(monStr.length() - 2, monStr.length());
        String dayStr = "00" + day;
        dayStr = dayStr.substring(dayStr.length() - 2, dayStr.length());

        endTime = Integer.parseInt(yearStr + monStr + dayStr);
        return endTime;
    }

    /**
     * 判断注册码是否正确
     */
    boolean judgeRegisterCode() {
        //解密先除以2
        int regCode = Integer.parseInt("0" + registercode.getText().toString()) / 2;
        String regStr = "000000" + regCode;
        regStr = regStr.substring(regStr.length() - 6, regStr.length());

        //获得Mac地址并取余：mac1+2+3+4 % 100
        String macStr = wifimac.substring(0, 2) + wifimac.substring(15, 17);
        int macInt = 0;
        for (int i = 0; i < 4; i++) {
            macInt = macInt + macStr.substring(i, i + 1).charAt(0);
        }
        macStr = "" + macInt % 100;
        if (macStr.length() < 2) macStr = "0" + macStr;
        //今天日期
        String day = getData().substring(6, 8);
        //判断
        if (macStr.equals(regStr.substring(0, 2)) && (day.equals(regStr.substring(4, 6))))
            return true;
        else
            return false;
    }

    /**
     * 显示手机MAC地址
     */
    void displayMacAdd() {
        String wifishoumo = "本机首两位MAC:" + wifimac.substring(0, 2) + " 末两位MAC:" + wifimac.substring(15, 17) + "\n" + "\n";
        // if(systmoshi1==0)
        // wifishoumo=wifishoumo+"提示：请将手机更改为自动时间";
        macAdd.setText(wifishoumo);
    }

    /**
     * 设置back监听
     */
    @Override
    public void onBackPressed() {
        exitApp();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
